<template>
  <div id="app">
    <router-view v-if="isRouterAlive"></router-view>
  </div>
</template>
<script>
import { eventBus } from '@/lib/eventBus.js';
export default {
  name: 'app',
  data() {
    return { isRouterAlive: true };
  },
  methods: {
    // 模拟重载方法
    reload() {
      // 将dom消失掉
      this.isRouterAlive = false;
      // 在nextTick之后将dom重现已达到刷新页面的效果
      this.$nextTick(() => {
        this.isRouterAlive = true;
      });
    },
    // 反回客户端,处理ios返回问题
    backtTHSForce() {
      //开启返回监听拦截
      window.callNativeHandler('notifyWebHandleEvent', {
        method: 'detainNotify',
        type: '1',
      });
      window.registerWebHandler('notifyWebHandleEvent', data => {
        if (data.method === 'detainNotify') {
          const { type } = data;
          let backType = 'webpage';
          if (type === '1' && window.getPlatform() === 'iphone') {
            // 返回
            backType = 'webview';
          } else {
            if (type === '2') {
              // 关闭
              backType = 'component';
            }
          }
          window.callNativeHandler('goback', { type: backType });
          // 触发后有时会失效，需要再次注册
          this.backtTHSForce();
        }
      });
    },
  },
  provide() {
    // 为整个项目提供
    return { reload: this.reload };
  },
  mounted() {
    window.registerWebListener('onShow', () => {
      // 用事件总线来下发函数，使项目只注册一次
      eventBus.$emit('onShow', true);
      _Global.isWindowShow = true;
    });
    window.registerWebListener('onHide', () => {
      eventBus.$emit('onShow', false);
      _Global.isWindowShow = false;
    });
    this.backtTHSForce();
  },
};
</script>
